package land_register;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;

import Tool.Properties_tool;



public class Message_sending extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter out = response.getWriter();   

		String phone = request.getParameter("phone");
		
		//获取真实路径
		Properties_tool pp = new Properties_tool(request.getSession().getServletContext().getRealPath("/WEB-INF/classes/land_register/Msg_send_set.properties"));
		//System.out.println(request.getSession().getServletContext().getRealPath("/WEB-INF/classes/land_register/Msg_send_set.properties"));
		
		//获取各项参数
		String appkey = pp.getvalue("appkey");         
		String url = pp.getvalue("url");
		String secret = pp.getvalue("secret");
		String autograph = pp.getvalue("autograph");
		//String autograph = "蓝色领域";
		
		String Template_id = pp.getvalue("Template_id");
		String product_name = pp.getvalue("product_name");

		int Verification_code;
		Verification_code = (int)(Math.random()*90000)+10000;      //产生验证码
		
		TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
		AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
		req.setExtend("0");     //设置返回状态码
		req.setSmsType("normal");
		req.setSmsFreeSignName(autograph);                        //设置签名
		req.setSmsParam("{'code':'"+Verification_code+"','product':'"+product_name+"'}");
		req.setRecNum(phone);
		req.setSmsTemplateCode(Template_id);                   //设置模版ID
		try {
			AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
			
			out.print("{\"Verification_code\":\""+Verification_code+"\","+"\"re\":"+rsp.getBody()+"}");    //输出json对象
			out.close();
		} catch (ApiException e) {
	
			e.printStackTrace();
		}
		
	}

}
